// src/composables/useTasks.js
import { computed } from 'vue'
import { useTaskStore } from '@/stores/taskStore'

export function useTasks() {
  const store = useTaskStore()
  
  // 按优先级筛选任务
  const filterTasksByPriority = (priority) => {
    return computed(() => {
      return store.tasks.filter(task => task.priority === priority)
    })
  }
  
  // 获取用户的任务统计
  const getUserTaskStats = (userId) => {
    return computed(() => {
      const userTasks = store.tasks.filter(task => task.assignedTo === userId)
      return {
        total: userTasks.length,
        completed: userTasks.filter(t => t.status === 'done').length,
        inProgress: userTasks.filter(t => t.status === 'doing').length
      }
    })
  }
  
  // 计算任务平均完成时间
  const calculateAverageCompletionTime = () => {
    const doneTasks = store.tasks.filter(t => t.status === 'done' && t.completedAt)
    if (doneTasks.length === 0) return 0
    
    const totalTime = doneTasks.reduce((sum, task) => {
      return sum + (new Date(task.completedAt) - new Date(task.createdAt))
    }, 0)
    
    return Math.round(totalTime / doneTasks.length / (1000 * 60 * 60 * 24)) // 转换为天数
  }
  
  return { 
    filterTasksByPriority, 
    getUserTaskStats,
    calculateAverageCompletionTime
  }
}